package org.git.movie.util;

import com.alibaba.fastjson.JSON;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @author zl
 * @date 2020/7/13 0013 16:19
 * @version: V1.0
 */
public class BaseController {


    protected <T> BaseResponse<T> ok(T data){
        return new BaseResponse(data);
    }


    public static void responseValidErrorMsg(HttpServletResponse response,String code,String msg){
        PrintWriter out = null;
        try{
            BaseResponse repJson = new BaseResponse(null);
            repJson.setStatus(code);
            repJson.setMessage(msg);

            response.setStatus(HttpStatus.OK.value() );
            out = response.getWriter();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=UTF-8");
            response.setHeader("pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            out.write( JSON.toJSONString(repJson) );
            return;
        } catch (Exception e) {
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
}
